<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Redis基础 | yili的个人小站</title><meta name="keywords" content="Redis,数据库"><meta name="author" content="yili"><meta name="copyright" content="yili"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="Redis基础五个基本Redis数据结构（官网信息） 首先对redis来说，所有的key（键）都是字符串。我们在谈基础数据结构时，讨论的是存储值的数据类型，主要包括常见的5种数据类型，分别是：String、List、Set、Zset、Hash。     结构类型 结构存储的值 结构的读写能力    String字符串 可以是字符串、整数或浮点数 对整个字符串或字符串的一部分进行操作；对整数或浮点数">
<meta property="og:type" content="article">
<meta property="og:title" content="Redis基础">
<meta property="og:url" content="https://yili979.gitee.io/blog/2021/10/05/nosql/Redis%E5%9F%BA%E7%A1%80/index.html">
<meta property="og:site_name" content="yili的个人小站">
<meta property="og:description" content="Redis基础五个基本Redis数据结构（官网信息） 首先对redis来说，所有的key（键）都是字符串。我们在谈基础数据结构时，讨论的是存储值的数据类型，主要包括常见的5种数据类型，分别是：String、List、Set、Zset、Hash。     结构类型 结构存储的值 结构的读写能力    String字符串 可以是字符串、整数或浮点数 对整个字符串或字符串的一部分进行操作；对整数或浮点数">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://i.loli.net/2021/10/04/R5mOBnMaeyZ7cpT.png">
<meta property="article:published_time" content="2021-10-04T16:06:51.000Z">
<meta property="article:modified_time" content="2021-10-04T16:13:22.916Z">
<meta property="article:author" content="yili">
<meta property="article:tag" content="Redis">
<meta property="article:tag" content="数据库">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://i.loli.net/2021/10/04/R5mOBnMaeyZ7cpT.png"><link rel="shortcut icon" href="/blog/img/favicon.ico"><link rel="canonical" href="https://yili979.gitee.io/blog/2021/10/05/nosql/Redis%E5%9F%BA%E7%A1%80/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/blog/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/blog/',
  algolia: undefined,
  localSearch: undefined,
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: {"chs_to_cht":"你已切换为繁体","cht_to_chs":"你已切换为简体","day_to_night":"你已切换为深色模式","night_to_day":"你已切换为浅色模式","bgLight":"#49b1f5","bgDark":"#121212","position":"bottom-left"},
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Redis基础',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2021-10-05 00:13:22'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    
    const detectApple = () => {
      if (GLOBAL_CONFIG_SITE.isHome && /iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
        document.documentElement.classList.add('apple')
      }
    }
    detectApple()
    })(window)</script><link rel="stylesheet" href="/blog/css/custom.css" media="defer" onload="this.media='all'"><meta name="generator" content="Hexo 5.4.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://i.loli.net/2021/10/05/BKpC6sFEo5Dl7qA.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">28</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">20</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">13</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://i.loli.net/2021/10/04/R5mOBnMaeyZ7cpT.png')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/blog/">yili的个人小站</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/blog/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/blog/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/blog/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/blog/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/blog/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/blog/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Redis基础</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2021-10-04T16:06:51.000Z" title="发表于 2021-10-05 00:06:51">2021-10-05</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2021-10-04T16:13:22.916Z" title="更新于 2021-10-05 00:13:22">2021-10-05</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/blog/categories/Redis/">Redis</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">3.4k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>12分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Redis基础"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="Redis基础"><a href="#Redis基础" class="headerlink" title="Redis基础"></a>Redis基础</h1><h2 id="五个基本Redis数据结构"><a href="#五个基本Redis数据结构" class="headerlink" title="五个基本Redis数据结构"></a>五个基本Redis数据结构</h2><p>（<a target="_blank" rel="noopener" href="https://www.redis.com.cn/redis-strings.html#redis-%E5%AD%97%E7%AC%A6%E4%B8%B2%E5%91%BD%E4%BB%A4-">官网信息</a>）</p>
<p>首先对redis来说，所有的key（键）都是字符串。我们在谈基础数据结构时，讨论的是存储值的数据类型，主要包括常见的5种数据类型，分别是：String、List、Set、Zset、Hash。</p>
<p><img src="https://i.loli.net/2021/10/05/fBEMh4A6RjH7LrU.jpg" alt="img"></p>
<table>
<thead>
<tr>
<th>结构类型</th>
<th>结构存储的值</th>
<th>结构的读写能力</th>
</tr>
</thead>
<tbody><tr>
<td><strong>String字符串</strong></td>
<td>可以是字符串、整数或浮点数</td>
<td>对整个字符串或字符串的一部分进行操作；对整数或浮点数进行自增或自减操作；</td>
</tr>
<tr>
<td><strong>List列表</strong></td>
<td>一个链表，链表上的每个节点都包含一个字符串</td>
<td>对链表的两端进行push和pop操作，读取单个或多个元素；根据值查找或删除元素；</td>
</tr>
<tr>
<td><strong>Set集合</strong></td>
<td>包含字符串的无序集合</td>
<td>字符串的集合，包含基础的方法有看是否存在添加、获取、删除；还包含计算交集、并集、差集等</td>
</tr>
<tr>
<td><strong>Hash散列</strong></td>
<td>包含键值对的无序散列表</td>
<td>包含方法有添加、获取、删除单个元素</td>
</tr>
<tr>
<td><strong>Zset有序集合</strong></td>
<td>和散列一样，用于存储键值对</td>
<td>字符串成员与浮点数分数之间的有序映射；元素的排列顺序由分数的大小决定；包含方法有添加、获取、删除单个元素以及根据分值范围或成员来获取元素</td>
</tr>
</tbody></table>
<blockquote>
<p>内容其实比较简单，我觉得理解的重点在于这个结构怎么用，能够用来做什么？所以我在梳理时，围绕<strong>图例</strong>，<strong>命令</strong>，<strong>执行</strong>和<strong>场景</strong>来阐述。@pdai</p>
</blockquote>
<h3 id="1-String-字符串"><a href="#1-String-字符串" class="headerlink" title="1.String 字符串"></a>1.String 字符串</h3><blockquote>
<p>String是redis中最基本的数据类型，一个key对应一个value。</p>
</blockquote>
<p>String类型是二进制安全的，意思是 redis 的 string 可以包含任何数据。如数字，字符串，jpg图片或者序列化的对象。</p>
<p>这个字符串不是正常意义上的字符串，而是以一种键值对的形式存在。</p>
<p>一般来说key值是一个字符串，而value值可以是任意的一种结构。</p>
<p>以下是一些用于在 Redis 中管理字符串的基本命令的列表：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/set.html">SET</a></td>
<td>设置指定 key 的值</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/get.html">GET</a></td>
<td>获取指定 key 的值</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/getrange.html">GETRANGE</a></td>
<td>返回 key 中字符串值的子字符</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/getset.html">GETSET</a></td>
<td>将给定 key 的值设为 value ，并返回 key 的旧值 ( old value )</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/getbit.html">GETBIT</a></td>
<td>对 key 所储存的字符串值，获取指定偏移量上的位 ( bit )</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/mget.html">MGET</a></td>
<td>获取所有(一个或多个)给定 key 的值</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/setbit.html">SETBIT</a></td>
<td>对 key 所储存的字符串值，设置或清除指定偏移量上的位(bit)</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/setex.html">SETEX</a></td>
<td>设置 key 的值为 value 同时将过期时间设为 seconds</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/setnx.html">SETNX</a></td>
<td>只有在 key 不存在时设置 key 的值</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/setrange.html">SETRANGE</a></td>
<td>从偏移量 offset 开始用 value 覆写给定 key 所储存的字符串值</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/strlen.html">STRLEN</a></td>
<td>返回 key 所储存的字符串值的长度</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/mset.html">MSET</a></td>
<td>同时设置一个或多个 key-value 对</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/msetnx.html">MSETNX</a></td>
<td>同时设置一个或多个 key-value 对</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/psetex.html">PSETEX</a></td>
<td>以毫秒为单位设置 key 的生存时间</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/incr.html">INCR</a></td>
<td>将 key 中储存的数字值增一</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/incrby.html">INCRBY</a></td>
<td>将 key 所储存的值加上给定的增量值 ( increment )</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/incrbyfloat.html">INCRBYFLOAT</a></td>
<td>将 key 所储存的值加上给定的浮点增量值 ( increment )</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/decr.html">DECR</a></td>
<td>将 key 中储存的数字值减一</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/decrby.html">DECRBY</a></td>
<td>将 key 所储存的值减去给定的减量值 ( decrement )</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/append.html">APPEND</a></td>
<td>将 value 追加到 key 原来的值的末尾</td>
</tr>
</tbody></table>
<h3 id="2-Hashes-哈希表"><a href="#2-Hashes-哈希表" class="headerlink" title="2.Hashes 哈希表"></a>2.Hashes 哈希表</h3><p><strong>Redis哈希命令</strong>：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hdel.html">HDEL</a></td>
<td>删除一个或多个哈希表字段</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hexists.html">HEXISTS</a></td>
<td>查看哈希表 key 中，指定的字段是否存在</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hget.html">HGET</a></td>
<td>获取存储在哈希表中指定字段的值</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hgetall.html">HGETALL</a></td>
<td>获取在哈希表中指定 key 的所有字段和值</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hincrby.html">HINCRBY</a></td>
<td>为哈希表 key 中的指定字段的整数值加上增量 increment</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hincrbyfloat.html">HINCRBYFLOAT</a></td>
<td>为哈希表 key 中的指定字段的浮点数值加上增量 increment</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hkeys.html">HKEYS</a></td>
<td>获取所有哈希表中的字段</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hlen.html">HLEN</a></td>
<td>获取哈希表中字段的数量</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hmget.html">HMGET</a></td>
<td>获取所有给定字段的值</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hmset.html">HMSET</a></td>
<td>同时将多个 field-value (域-值)对设置到哈希表 key 中</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hset.html">HSET</a></td>
<td>将哈希表 key 中的字段 field 的值设为 value</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hsetnx.html">HSETNX</a></td>
<td>只有在字段 field 不存在时，设置哈希表字段的值</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hvals.html">HVALS</a></td>
<td>获取哈希表中所有值</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hscan.html">HSCAN</a></td>
<td>迭代哈希表中的键值对</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/hstrlen.html">HSTRLEN</a></td>
<td>返回哈希表 key 中， 与给定域 field 相关联的值的字符串长度</td>
</tr>
</tbody></table>
<figure class="highlight xml"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">&gt; hset user one 1</span><br><span class="line">1</span><br><span class="line">&gt; hget user one</span><br><span class="line">&quot;1&quot;</span><br></pre></td></tr></table></figure>

<h3 id="3-List-列表"><a href="#3-List-列表" class="headerlink" title="3.List 列表"></a>3.List 列表</h3><p>使用List结构，我们可以轻松地实现最新消息排队功能（比如新浪微博的TimeLine）。List的另一个应用就是消息队列，可以利用List的 PUSH 操作，将任务存放在List中，然后工作线程再用 POP 操作将任务取出进行执行。</p>
<p>下表列出了列表相关命令：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/blpop.html">BLPOP</a></td>
<td>移出并获取列表的第一个元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/brpop.html">BRPOP</a></td>
<td>移出并获取列表的最后一个元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/brpoplpush.html">BRPOPLPUSH</a></td>
<td>从列表中弹出一个值，并将该值插入到另外一个列表中并返回它</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/lindex.html">LINDEX</a></td>
<td>通过索引获取列表中的元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/linsert.html">LINSERT</a></td>
<td>在列表的元素前或者后插入元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/llen.html">LLEN</a></td>
<td>获取列表长度</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/lpop.html">LPOP</a></td>
<td>移出并获取列表的第一个元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/lpush.html">LPUSH</a></td>
<td>将一个或多个值插入到列表头部</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/lpushx.html">LPUSHX</a></td>
<td>将一个值插入到已存在的列表头部</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/lrange.html">LRANGE</a></td>
<td>获取列表指定范围内的元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/lrem.html">LREM</a></td>
<td>移除列表元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/lset.html">LSET</a></td>
<td>通过索引设置列表元素的值</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/ltrim.html">LTRIM</a></td>
<td>对一个列表进行修剪(trim)</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/rpop.html">RPOP</a></td>
<td>移除并获取列表最后一个元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/rpoplpush.html">RPOPLPUSH</a></td>
<td>移除列表的最后一个元素，并将该元素添加到另一个列表并返回</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/rpush.html">RPUSH</a></td>
<td>在列表中添加一个或多个值</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/rpushx.html">RPUSHX</a></td>
<td>为已存在的列表添加值</td>
</tr>
</tbody></table>
<h3 id="4-Set-集合"><a href="#4-Set-集合" class="headerlink" title="4.Set 集合"></a>4.Set 集合</h3><p>下表列出了 Redis 集合相关命令：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/sadd.html">SADD</a></td>
<td>向集合添加一个或多个成员</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/scard.html">SCARD</a></td>
<td>获取集合的成员数</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/sdiff.html">SDIFF</a></td>
<td>返回给定所有集合的差集</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/sdiffstore.html">SDIFFSTORE</a></td>
<td>返回给定所有集合的差集并存储在 destination 中</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/sinter.html">SINTER</a></td>
<td>返回给定所有集合的交集</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/sinterstore.html">SINTERSTORE</a></td>
<td>返回给定所有集合的交集并存储在 destination 中</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/sismember.html">SISMEMBER</a></td>
<td>判断 member 元素是否是集合 key 的成员</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/smembers.html">SMEMBERS</a></td>
<td>返回集合中的所有成员</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/smove.html">SMOVE</a></td>
<td>将 member 元素从 source 集合移动到 destination 集合</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/spop.html">SPOP</a></td>
<td>移除并返回集合中的一个随机元素</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/srandmember.html">SRANDMEMBER</a></td>
<td>返回集合中一个或多个随机数</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/srem.html">SREM</a></td>
<td>移除集合中一个或多个成员</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/sunion.html">SUNION</a></td>
<td>返回所有给定集合的并集</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/sunionstore.html">SUNIONSTORE</a></td>
<td>所有给定集合的并集存储在 destination 集合中</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/sscan.html">SSCAN</a></td>
<td>迭代集合中的元素</td>
</tr>
</tbody></table>
<h3 id="5-Sorted-Sets-有序集合"><a href="#5-Sorted-Sets-有序集合" class="headerlink" title="5.Sorted Sets 有序集合"></a>5.Sorted Sets 有序集合</h3><p>有序集合就是将HashMap中的value值修改为一个double类型的分数，可以通过分数对集合进行排序，得到系列的结果。</p>
<p>下表列出了 Redis 有序集合的基本命令</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zadd.html">ZADD</a></td>
<td>向有序集合添加一个或多个成员，或者更新已存在成员的分数</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zcard.html">ZCARD</a></td>
<td>获取有序集合的成员数</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zcount.html">ZCOUNT</a></td>
<td>计算在有序集合中指定区间分数的成员数</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zincrby.html">ZINCRBY</a></td>
<td>有序集合中对指定成员的分数加上增量 increment</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zinterstore.html">ZINTERSTORE</a></td>
<td>计算给定的一个或多个有序集的交集并将结果集存储在新的有序集合 key 中</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zlexcount.html">ZLEXCOUNT</a></td>
<td>在有序集合中计算指定字典区间内成员数量</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zrange.html">ZRANGE</a></td>
<td>通过索引区间返回有序集合成指定区间内的成员</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zrangebylex.html">ZRANGEBYLEX</a></td>
<td>通过字典区间返回有序集合的成员</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zrangebyscore.html">ZRANGEBYSCORE</a></td>
<td>通过分数返回有序集合指定区间内的成员</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zrank.html">ZRANK</a></td>
<td>返回有序集合中指定成员的索引</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zrem.html">ZREM</a></td>
<td>移除有序集合中的一个或多个成员</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zremrangebylex.html">ZREMRANGEBYLEX</a></td>
<td>移除有序集合中给定的字典区间的所有成员</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zremrangebyrank.html">ZREMRANGEBYRANK</a></td>
<td>移除有序集合中给定的排名区间的所有成员</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zremrangebyscore.html">ZREMRANGEBYSCORE</a></td>
<td>移除有序集合中给定的分数区间的所有成员</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zrevrange.html">ZREVRANGE</a></td>
<td>返回有序集中指定区间内的成员，通过索引，分数从高到底</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zrevrangebyscore.html">ZREVRANGEBYSCORE</a></td>
<td>返回有序集中指定分数区间内的成员，分数从高到低排序</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zrevrank.html">ZREVRANK</a></td>
<td>返回有序集合中指定成员的排名，有序集成员按分数值递减(从大到小)排序</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zscore.html">ZSCORE</a></td>
<td>返回有序集中，成员的分数值</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zunionstore.html">ZUNIONSTORE</a></td>
<td>计算一个或多个有序集的并集，并存储在新的 key 中</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/zscan.html">ZSCAN</a></td>
<td>迭代有序集合中的元素（包括元素成员和元素分值）</td>
</tr>
</tbody></table>
<h2 id="三个额外的数据结构"><a href="#三个额外的数据结构" class="headerlink" title="三个额外的数据结构"></a>三个额外的数据结构</h2><h3 id="1-Redis-HyperLogLog"><a href="#1-Redis-HyperLogLog" class="headerlink" title="1.Redis HyperLogLog"></a>1.Redis HyperLogLog</h3><p>这是一个很特殊的数据结构，一般用于统计一个结构中含有的数据量（通过pfcount来进行统计）</p>
<p>下表列出了列表相关命令：</p>
<table>
<thead>
<tr>
<th>命令</th>
<th>描述</th>
</tr>
</thead>
<tbody><tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/pfadd.html">PFADD</a></td>
<td>添加指定元素到 HyperLogLog 中。</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/pfcount.html">PFCOUNT</a></td>
<td>返回给定 HyperLogLog 的基数估算值。</td>
</tr>
<tr>
<td><a target="_blank" rel="noopener" href="https://www.redis.com.cn/commands/pfmerge.html">PFMERGE</a></td>
<td>将多个 HyperLogLog 合并为一个 HyperLogLog</td>
</tr>
</tbody></table>
<h3 id="2-geospatial-地理位置"><a href="#2-geospatial-地理位置" class="headerlink" title="2.geospatial (地理位置)"></a>2.geospatial (地理位置)</h3><blockquote>
<p>Redis 的 Geo 在 Redis 3.2 版本就推出了! 这个功能可以推算地理位置的信息: 两地之间的距离, 方圆几里的人</p>
</blockquote>
<p><strong>geoadd</strong></p>
<blockquote>
<p>添加地理位置</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; geoadd china:city 118.76 32.04 manjing 112.55 37.86 taiyuan 123.43 41.80 shenyang</span><br><span class="line">(<span class="built_in">integer</span>) 3</span><br><span class="line">127.0.0.1:6379&gt; geoadd china:city 144.05 22.52 shengzhen 120.16 30.24 hangzhou 108.96 34.26 xian</span><br><span class="line">(<span class="built_in">integer</span>) 3</span><br><span class="line">    </span><br></pre></td></tr></table></figure>

<p><strong>规则</strong></p>
<p>两级无法直接添加，我们一般会下载城市数据(这个网址可以查询 GEO： <a target="_blank" rel="noopener" href="http://www.jsons.cn/lngcode)%EF%BC%81">http://www.jsons.cn/lngcode)！</a></p>
<ul>
<li>有效的经度从-180度到180度。</li>
<li>有效的纬度从-85.05112878度到85.05112878度。</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="comment"># 当坐标位置超出上述指定范围时，该命令将会返回一个错误。</span></span><br><span class="line">127.0.0.1:6379&gt; geoadd china:city 39.90 116.40 beijin</span><br><span class="line">(error) ERR invalid longitude,latitude pair 39.900000,116.400000</span><br></pre></td></tr></table></figure>

<p><strong>geopos</strong></p>
<blockquote>
<p>获取指定的成员的经度和纬度</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; geopos china:city taiyuan manjing</span><br><span class="line">1) 1) <span class="string">&quot;112.54999905824661255&quot;</span></span><br><span class="line">   1) <span class="string">&quot;37.86000073876942196&quot;</span></span><br><span class="line">2) 1) <span class="string">&quot;118.75999957323074341&quot;</span></span><br><span class="line">   1) <span class="string">&quot;32.03999960287850968&quot;</span></span><br></pre></td></tr></table></figure>

<p>获得当前定位, 一定是一个坐标值</p>
<p> <strong>geodist</strong></p>
<blockquote>
<p>如果不存在, 返回空</p>
</blockquote>
<p>单位如下</p>
<ul>
<li>m</li>
<li>km</li>
<li>mi 英里</li>
<li>ft 英尺</li>
</ul>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; geodist china:city taiyuan shenyang m</span><br><span class="line"><span class="string">&quot;1026439.1070&quot;</span></span><br><span class="line">127.0.0.1:6379&gt; geodist china:city taiyuan shenyang km</span><br><span class="line"><span class="string">&quot;1026.4391&quot;</span></span><br></pre></td></tr></table></figure>

<p><strong>georadius</strong></p>
<blockquote>
<p>附近的人 ==&gt; 获得所有附近的人的地址, 定位, 通过半径来查询</p>
</blockquote>
<p>获得指定数量的人</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; georadius china:city 110 30 1000 km			以 100,30 这个坐标为中心, 寻找半径为1000km的城市</span><br><span class="line">1) <span class="string">&quot;xian&quot;</span></span><br><span class="line">2) <span class="string">&quot;hangzhou&quot;</span></span><br><span class="line">3) <span class="string">&quot;manjing&quot;</span></span><br><span class="line">4) <span class="string">&quot;taiyuan&quot;</span></span><br><span class="line">127.0.0.1:6379&gt; georadius china:city 110 30 500 km</span><br><span class="line">1) <span class="string">&quot;xian&quot;</span></span><br><span class="line">127.0.0.1:6379&gt; georadius china:city 110 30 500 km withdist</span><br><span class="line">1) 1) <span class="string">&quot;xian&quot;</span></span><br><span class="line">   2) <span class="string">&quot;483.8340&quot;</span></span><br><span class="line">127.0.0.1:6379&gt; georadius china:city 110 30 1000 km withcoord withdist count 2</span><br><span class="line">1) 1) <span class="string">&quot;xian&quot;</span></span><br><span class="line">   2) <span class="string">&quot;483.8340&quot;</span></span><br><span class="line">   3) 1) <span class="string">&quot;108.96000176668167114&quot;</span></span><br><span class="line">      2) <span class="string">&quot;34.25999964418929977&quot;</span></span><br><span class="line">2) 1) <span class="string">&quot;manjing&quot;</span></span><br><span class="line">   2) <span class="string">&quot;864.9816&quot;</span></span><br><span class="line">   3) 1) <span class="string">&quot;118.75999957323074341&quot;</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>

<p>参数 key 经度 纬度 半径 单位 [显示结果的经度和纬度] [显示结果的距离] [显示的结果的数量]</p>
<p><strong>georadiusbymember</strong></p>
<blockquote>
<p>显示与指定成员一定半径范围内的其他成员</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; georadiusbymember china:city taiyuan 1000 km</span><br><span class="line">1) <span class="string">&quot;manjing&quot;</span></span><br><span class="line">2) <span class="string">&quot;taiyuan&quot;</span></span><br><span class="line">3) <span class="string">&quot;xian&quot;</span></span><br><span class="line">127.0.0.1:6379&gt; georadiusbymember china:city taiyuan 1000 km withcoord withdist count 2</span><br><span class="line">1) 1) <span class="string">&quot;taiyuan&quot;</span></span><br><span class="line">   2) <span class="string">&quot;0.0000&quot;</span></span><br><span class="line">   3) 1) <span class="string">&quot;112.54999905824661255&quot;</span></span><br><span class="line">      2) <span class="string">&quot;37.86000073876942196&quot;</span></span><br><span class="line">2) 1) <span class="string">&quot;xian&quot;</span></span><br><span class="line">   2) <span class="string">&quot;514.2264&quot;</span></span><br><span class="line">   3) 1) <span class="string">&quot;108.96000176668167114&quot;</span></span><br><span class="line">      2) <span class="string">&quot;34.25999964418929977&quot;</span>   </span><br></pre></td></tr></table></figure>

<p>参数与 georadius 一样</p>
<p><strong>geohash(较少使用)</strong></p>
<blockquote>
<p>该命令返回11个字符的hash字符串</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; geohash china:city taiyuan shenyang</span><br><span class="line">1) <span class="string">&quot;ww8p3hhqmp0&quot;</span></span><br><span class="line">2) <span class="string">&quot;wxrvb9qyxk0&quot;</span>  </span><br></pre></td></tr></table></figure>

<p>将二维的经纬度转换为一维的字符串, 如果两个字符串越接近, 则距离越近</p>
<p><strong>底层</strong></p>
<blockquote>
<p>geo底层的实现原理实际上就是Zset, 我们可以通过Zset命令来操作geo</p>
</blockquote>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; <span class="built_in">type</span> china:city</span><br><span class="line">zset</span><br></pre></td></tr></table></figure>

<p>查看全部元素 删除指定的元素</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; zrange china:city 0 -1 withscores</span><br><span class="line"> 1) <span class="string">&quot;xian&quot;</span></span><br><span class="line"> 2) <span class="string">&quot;4040115445396757&quot;</span></span><br><span class="line"> 3) <span class="string">&quot;hangzhou&quot;</span></span><br><span class="line"> 4) <span class="string">&quot;4054133997236782&quot;</span></span><br><span class="line"> 5) <span class="string">&quot;manjing&quot;</span></span><br><span class="line"> 6) <span class="string">&quot;4066006694128997&quot;</span></span><br><span class="line"> 7) <span class="string">&quot;taiyuan&quot;</span></span><br><span class="line"> 8) <span class="string">&quot;4068216047500484&quot;</span></span><br><span class="line"> 9) <span class="string">&quot;shenyang&quot;</span></span><br><span class="line">1)  <span class="string">&quot;4072519231994779&quot;</span></span><br><span class="line">2)  <span class="string">&quot;shengzhen&quot;</span></span><br><span class="line">3)  <span class="string">&quot;4154606886655324&quot;</span></span><br><span class="line">127.0.0.1:6379&gt; zrem china:city manjing</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; zrange china:city 0 -1</span><br><span class="line">1) <span class="string">&quot;xian&quot;</span></span><br><span class="line">2) <span class="string">&quot;hangzhou&quot;</span></span><br><span class="line">3) <span class="string">&quot;taiyuan&quot;</span></span><br><span class="line">4) <span class="string">&quot;shenyang&quot;</span></span><br><span class="line">5) <span class="string">&quot;shengzhen&quot;</span></span><br></pre></td></tr></table></figure>

<h3 id="3-bitmap"><a href="#3-bitmap" class="headerlink" title="3.bitmap"></a>3.bitmap</h3><p>bitmap是进行位存储的一个函数。</p>
<p>通过setbit来对map进行存储映射，通过getbit来对每一个key来进行获取，可以看作一个bit类型的数组，可以通过单个字节来描述一个节点的二元状态。</p>
<p>并且可以使用bitcount来对总计的记录进行统计：</p>
<figure class="highlight sh"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br></pre></td><td class="code"><pre><span class="line">127.0.0.1:6379&gt; setbit sign 0 1</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line">127.0.0.1:6379&gt; setbit sign 1 1</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line">127.0.0.1:6379&gt; setbit sign 2 0</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line">127.0.0.1:6379&gt; setbit sign 3 1</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line">127.0.0.1:6379&gt; setbit sign 4 0</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line">127.0.0.1:6379&gt; setbit sign 5 0</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line">127.0.0.1:6379&gt; setbit sign 6 1</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; getbit sign 3</span><br><span class="line">(<span class="built_in">integer</span>) 1</span><br><span class="line">127.0.0.1:6379&gt; getbit sign 5</span><br><span class="line">(<span class="built_in">integer</span>) 0</span><br><span class="line"></span><br><span class="line">127.0.0.1:6379&gt; bitcount sign <span class="comment"># 统计这周的打卡记录，就可以看到是否有全勤！</span></span><br><span class="line">(<span class="built_in">integer</span>) 3</span><br></pre></td></tr></table></figure>



<hr>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">yili</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://yili979.gitee.io/blog/2021/10/05/nosql/Redis%E5%9F%BA%E7%A1%80/">https://yili979.gitee.io/blog/2021/10/05/nosql/Redis%E5%9F%BA%E7%A1%80/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://yili979.gitee.io/blog" target="_blank">yili的个人小站</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/blog/tags/Redis/">Redis</a><a class="post-meta__tags" href="/blog/tags/%E6%95%B0%E6%8D%AE%E5%BA%93/">数据库</a></div><div class="post_share"><div class="social-share" data-image="https://i.loli.net/2021/10/04/R5mOBnMaeyZ7cpT.png" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/blog/2021/10/05/jvm/JVM-%E7%B1%BB%E5%8A%A0%E8%BD%BD%E8%BF%87%E7%A8%8B/"><img class="prev-cover" src="https://i.loli.net/2021/10/05/Lf36O8MDvnZux2A.png" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">JVM-类加载过程</div></div></a></div><div class="next-post pull-right"><a href="/blog/2021/10/05/java/Java8%E6%B5%81%E5%A4%84%E7%90%86/"><img class="next-cover" src="https://i.loli.net/2021/10/04/7PWqmtGLMs3hign.jpg" onerror="onerror=null;src='/blog/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Java8流处理</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://i.loli.net/2021/10/05/BKpC6sFEo5Dl7qA.jpg" onerror="this.onerror=null;this.src='/blog/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">yili</div><div class="author-info__description">身骑白马</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/blog/archives/"><div class="headline">文章</div><div class="length-num">28</div></a></div><div class="card-info-data-item is-center"><a href="/blog/tags/"><div class="headline">标签</div><div class="length-num">20</div></a></div><div class="card-info-data-item is-center"><a href="/blog/categories/"><div class="headline">分类</div><div class="length-num">13</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/qiuyuyin"><i class="fab fa-github"></i><span>github🍟</span></a></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">啦啦啦，写的不好还请见谅😉<div class="twopeople"><div class="twopeople"><div class="container"style="height:200px;"><canvas class="illo"width="800"height="800"style="max-width: 200px; max-height: 200px; touch-action: none; width: 640px; height: 640px;"></canvas></div><script src="js/twopeople1.js"></script><script src="js/zdog.dist.js"></script><script id="rendered-js"src="js/twopeople.js"></script><style>.twopeople{margin:0;align-items:center;justify-content:center;text-align:center}canvas{display:block;margin:0 auto;cursor:move}</style></div></div></div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#Redis%E5%9F%BA%E7%A1%80"><span class="toc-text">Redis基础</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%94%E4%B8%AA%E5%9F%BA%E6%9C%ACRedis%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84"><span class="toc-text">五个基本Redis数据结构</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-String-%E5%AD%97%E7%AC%A6%E4%B8%B2"><span class="toc-text">1.String 字符串</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-Hashes-%E5%93%88%E5%B8%8C%E8%A1%A8"><span class="toc-text">2.Hashes 哈希表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-List-%E5%88%97%E8%A1%A8"><span class="toc-text">3.List 列表</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#4-Set-%E9%9B%86%E5%90%88"><span class="toc-text">4.Set 集合</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#5-Sorted-Sets-%E6%9C%89%E5%BA%8F%E9%9B%86%E5%90%88"><span class="toc-text">5.Sorted Sets 有序集合</span></a></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E4%B8%AA%E9%A2%9D%E5%A4%96%E7%9A%84%E6%95%B0%E6%8D%AE%E7%BB%93%E6%9E%84"><span class="toc-text">三个额外的数据结构</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#1-Redis-HyperLogLog"><span class="toc-text">1.Redis HyperLogLog</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#2-geospatial-%E5%9C%B0%E7%90%86%E4%BD%8D%E7%BD%AE"><span class="toc-text">2.geospatial (地理位置)</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#3-bitmap"><span class="toc-text">3.bitmap</span></a></li></ol></li></ol></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/03/08/something-about-crypto/" title="something about crypto"><img src="https://yili979.oss-cn-beijing.aliyuncs.com/img/image-20220124000713258.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="something about crypto"/></a><div class="content"><a class="title" href="/blog/2022/03/08/something-about-crypto/" title="something about crypto">something about crypto</a><time datetime="2022-03-08T12:01:13.000Z" title="发表于 2022-03-08 20:01:13">2022-03-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/03/08/golang%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/" title="golang内存管理"><img src="https://yili979.oss-cn-beijing.aliyuncs.com/img/202203081958270.png" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="golang内存管理"/></a><div class="content"><a class="title" href="/blog/2022/03/08/golang%E5%86%85%E5%AD%98%E7%AE%A1%E7%90%86/" title="golang内存管理">golang内存管理</a><time datetime="2022-03-08T11:55:30.000Z" title="发表于 2022-03-08 19:55:30">2022-03-08</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/blog/2022/03/08/%E9%89%B4%E6%9D%83%E7%B3%BB%E7%BB%9F/" title="鉴权系统"><img src="https://yili979.oss-cn-beijing.aliyuncs.com/img/202203081950401.jpeg" onerror="this.onerror=null;this.src='/blog/img/404.jpg'" alt="鉴权系统"/></a><div class="content"><a class="title" href="/blog/2022/03/08/%E9%89%B4%E6%9D%83%E7%B3%BB%E7%BB%9F/" title="鉴权系统">鉴权系统</a><time datetime="2022-03-08T11:49:53.000Z" title="发表于 2022-03-08 19:49:53">2022-03-08</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By yili</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/blog/js/utils.js"></script><script src="/blog/js/main.js"></script><script src="https://cdn.jsdelivr.net/npm/node-snackbar/dist/snackbar.min.js"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script defer="defer" id="fluttering_ribbon" mobile="false" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/canvas-fluttering-ribbon.min.js"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>